package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author oldwine
 * @date 2022/6/16
 * @Version 1.0
 */
@SpringBootTest
public class EsDocumentDemo {
    @Autowired
    private IHotelService hotelService;

    private RestHighLevelClient client;

    @Test
    public void addDocumentTest() throws IOException {
        //1.从数据库中根据id查询数据并封装成文档数据  ===> Hotel ==> HotelDoc
        //1.1根据id查询Hotel数据
        Hotel hotel = hotelService.getById(38665L);
        //1.2 将Hotel对象封装成HotelDoc对象
        HotelDoc hotelDoc = new HotelDoc(hotel);
        //1.3 将HotelDoc对象转成Json格式字符串
        String jsonString = JSON.toJSONString(hotelDoc);

        //2.创建请求对象,封装请求数据
        //2.1 创建请求对象
        IndexRequest request = new IndexRequest("hotel");
        //2.2 设置文档id和内容
        request.id(hotelDoc.getId()+ "");
        request.source(jsonString, XContentType.JSON);
        //3.客户端发送请求
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        //4.处理响应数据
        DocWriteResponse.Result result = response.getResult();
        System.out.println(result);
    }

    @Test
    public void getDocumentTest() throws IOException {
        //1.创建请求对象
        GetRequest request = new GetRequest("hotel", "38665");
        //2.发送请求数据
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        //3.处理响应数据
        String json = response.getSourceAsString();
        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        System.out.println("hotelDoc = " + hotelDoc);
    }

    @Test
    public void deleteDocumentTest() throws IOException {
        //1.创建请求对象
        DeleteRequest request = new DeleteRequest("hotel", "38665");
        //2.发送请求
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        //3.处理响应数据
        System.out.println(response.getResult());

    }

    @Test
    public void updateDocumentTest() throws IOException {
        //1.创建请求对象
        UpdateRequest request = new UpdateRequest("hotel", "38665");
        request.doc(
                "price", "888",
                "starName", "二星"
        );
        //2.发送请求
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        //3.处理响应数据
        System.out.println(response.getResult());
    }

    @Test
    public void addBatchDocumentTest() throws IOException {
        // 批量查询酒店数据
        List<Hotel> hotels = hotelService.list();

        // 1.创建Request
        BulkRequest request = new BulkRequest();
        // 2.准备参数，添加多个新增的Request
        for (Hotel hotel : hotels) {
            // 2.1.转换为文档类型HotelDoc
            HotelDoc hotelDoc = new HotelDoc(hotel);
            // 2.2.创建新增文档的Request对象
            request.add(new IndexRequest("hotel")
                    .id(hotelDoc.getId().toString())
                    .source(JSON.toJSONString(hotelDoc), XContentType.JSON));
        }
        // 3.发送请求
        client.bulk(request, RequestOptions.DEFAULT);

    }

    @BeforeEach
    public void init() {
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://122.112.171.22:9200")
        ));
    }

    @AfterEach
    public void close() {
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



}
